package com.aiwei.tdjk.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by Sun on 2017/5/3.
 */
public class DateUtil {
    /**
     * 判断当前日该是什么日期
     *
     * @param startDay 开始时段的时间
     * @param day      后台传过来的天数
     * @param time     后台传过来的开始时间
     */
    public static Date getFristTime(Date startDay, Integer day, Date time) throws Exception {
        String dayTime = new SimpleDateFormat("dd").format(startDay);
        Calendar c = Calendar.getInstance();
        c.setTime(startDay);
        Integer year = c.get(Calendar.YEAR);
        Integer month = c.get(Calendar.MONTH) + 1;
        String newDay = year + "-" + month + "-" + day + " " + new SimpleDateFormat("HH:mm:ss").format(time);
        Date fristTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(newDay);
        if (day < Integer.parseInt(dayTime)) {
            fristTime = getNextMonth(fristTime, 1);
        }
        return fristTime;
    }

    /**
     * 判断当前日期是星期几
     *
     * @param pTime 修要判断的时间
     * @return dayForWeek 判断结果
     * @Exception 发生异常
     */
    public static int dayForWeek(Date pTime) {
        Calendar c = Calendar.getInstance();
        c.setTime(pTime);
        int dayForWeek = 0;
        if (c.get(Calendar.DAY_OF_WEEK) == 1) {
            dayForWeek = 7;
        } else {
            dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
        }
        return dayForWeek;
    }

    //获取当前时间几天后的时间
    public static Date getNextDay(Date date, int nexDay) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, +nexDay);//+1今天的时间加一天
        date = calendar.getTime();
        return date;
    }

    //获取当前时间几个月后的时间
    public static Date getNextMonth(Date date, int nexMonth) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, +nexMonth);//+1当前时间一月后的时间
        date = calendar.getTime();
        return date;
    }
    //获取拼接后的时间
    public static Date getNewDay(Date startDay, Date endDay) throws Exception {
        DateFormat df = DateFormat.getTimeInstance();//只显示出时分秒
        String s1 = df.format(endDay);
        DateFormat df1 = DateFormat.getDateInstance();//只显示年月日
        String s2 = df1.format(startDay);
        String s3 = s2 + " " + s1;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.parse(s3);
    }

    /**
     * 判断当前日期后的第一个星期几是多少号
     *
     * @param startDay 开始的时间
     * @return dayOfWeek 后的星期几
     * @Exception 发生异常
     */
    public static Date getDate(Date startDay, int dayOfWeek, Date time) throws Exception {
        Date getDay = null;
        //开始的时间星期几
        int dayForWeek = dayForWeek(startDay);
        if (dayForWeek > dayOfWeek) {
            int day = 7 + dayOfWeek - dayForWeek;
            getDay = getNextDay(startDay, day);
        } else if (dayForWeek == dayOfWeek) {
            getDay = startDay;
        } else {
            getDay = getNextDay(startDay, (dayOfWeek - dayForWeek));
        }
        return getNewDay(getDay, time);
    }

    //冒泡比较list<Date> 里面的一段时间和其他时间段都不相同
    public static boolean judgeTime(List<Date> startDays, List<Date> endDays, List<Integer> days) throws Exception {
        boolean lang = true;
        for (int i = 0; i < startDays.size() - 1; i++) {    //最多做n-1趟排序
            for (int j = i; j < startDays.size() - 1; j++) {
                // System.out.println("=======0.0>"+days.get(i)+"=======1.1>"+days.get(j+1));
                if (days.get(i) == days.get(j + 1)) {  //相同的周或天才作比较
                    Long startDay = startDays.get(i).getTime();
                    Long endDay = endDays.get(i).getTime();
                    Long startNextDay = startDays.get(j + 1).getTime();
                    Long endNextDay = endDays.get(j + 1).getTime();
                    if (startDay <= endNextDay && endDay >= startNextDay) {
                        lang = false;
                        break;
                    }
                }
            }
            if (!lang) break;
        }
        return lang;
    }


    public static List<String> getYearMonth(Date From, Date End) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String StartDate = simpleDateFormat.format(From);
        String EndDate = simpleDateFormat.format(End);
        List<String> DateList = new ArrayList<>();
        Date d1 = new SimpleDateFormat("yyyy-MM-dd").parse(StartDate);//定义起始日期
        Date d2 = new SimpleDateFormat("yyyy-MM-dd").parse(EndDate);//定义结束日期
        Calendar dd = Calendar.getInstance();//定义日期实例
        dd.setTime(d1);//设置日期起始时间
        while (dd.getTime().before(d2)) {//判断是否到结束日期
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            String str = sdf.format(dd.getTime());
            DateList.add(str);
            dd.add(Calendar.MONTH, 1);//进行当前日期月份加1
        }
        return DateList;
    }

    public static String changeDate(String date) {
        String string[] = date.split("-");
        int year = Integer.parseInt(string[0]);
        int month = Integer.parseInt(string[1]);
        int day = Integer.parseInt(string[2]);
        switch (month) {
            case 4:
            case 6:
            case 9:
            case 11:
                if (day == 31) {
                    day = 30;
                }
                break;
            case 2:
                if (judgeDate(year) && month == 2 && day >= 30) {
                    day = 29;
                    break;
                } else if (!judgeDate(year) && month == 2 && day >= 30) {
                    day = 28;
                    break;
                }
            default:
                break;
        }
        return String.valueOf(year) + "-" + String.valueOf(month) + "-" + String.valueOf(day);
    }

    /**
     * 判断是否闰年
     *
     * @param year
     * @return
     */
    public static boolean judgeDate(int year) {
        if ((year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0))) {
            return true;
        } else {
            return false;
        }
    }
    /**
     * 时间转化为时分秒
     *
     * @param date
     * @return
     */
    public static String completeDate(Date date) {
        if(date!=null){
            try {
                return new SimpleDateFormat("yyyy/MM/dd hh:mm:ss").format(date);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        return "";
    }
    /**
     * 时间转化为年月日
     *
     * @param date
     * @return
     */
    public static String dateDate(Date date) {
        if(date!=null){
            try {
                return new SimpleDateFormat("yyyy年MM月dd日").format(date);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        return "";
    }
    /**
     * 时间转化为年月日
     *
     * @param date
     * @return
     */
    public static String date2String(Date date) {
        if(date!=null){
            try {
                return new SimpleDateFormat("yyyy-MM-dd").format(date);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        return "";
    }

        public static void main(String[] args) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = format.parse("2017-1-28 12:30:00");
            Date date1 = format.parse("2017-1-28 10:30:00");
            Date date2 = format.parse("2017-1-28 15:30:00");
            Date edate = format.parse("2017-1-28 15:30:00");
            Date edate1 = format.parse("2017-1-28 13:30:00");
            Date edate2 = format.parse("2017-1-28 17:30:00");
            List<Date> list1=new ArrayList<>();
            list1.add(date);
            list1.add(date1);
            list1.add(date2);
            List<Date> list2=new ArrayList<>();
            list2.add(edate);
            list2.add(edate1);
            list2.add(edate2);
            System.out.println(dayForWeek(new Date()));
            System.out.println(getNextDay(new Date(),3));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static Date conversionDate(Date date){
        //获取系统当前时间并转化为年月日格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String s = sdf.format(date);
        Date nowDate=null;
        //系统当前时间
        try {
            nowDate =  sdf.parse(s);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return nowDate;
    }

    /**
     * 获取两个时间相隔的年数
     * @param date1
     * @param date2
     * @return
     * @throws ParseException
     */
    public static int getYearOfIntervalTime(Date date1,Date date2) throws ParseException {
        //转换成字符串
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        if(null!=date1 && null!=date2){
            String str1 = sdf.format(date1);
            String str2 = sdf.format(date2);
            GetIntervalFromNow intervalFromNow = new GetIntervalFromNow(str1,str2);
            return intervalFromNow.getYear();
        }else{
            return 0;
        }

    }

    /**
     * 获取两个时间相隔的月数
     * @param date1
     * @param date2
     * @return
     * @throws ParseException
     */
    public static Integer getMonthOfIntervalTime(Date date1,Date date2) throws ParseException {
        //转换成字符串
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        if(null!=date1 && null!=date2){
            String str1 = sdf.format(date1);
            String str2 = sdf.format(date2);
            GetIntervalFromNow intervalFromNow = new GetIntervalFromNow(str1,str2);
            return intervalFromNow.getMonth();
        }else{
            return 0;
        }
    }
    /**
     * 获取两个时间相隔的天数
     * @param date1
     * @param date2
     * @return
     * @throws ParseException
     */
    public static Integer getDayOfIntervalTime(Date date1,Date date2) throws ParseException {
        //转换成字符串
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        if(null!=date1 && null!=date2){
            String str1 = sdf.format(date1);
            String str2 = sdf.format(date2);
            GetIntervalFromNow intervalFromNow = new GetIntervalFromNow(str1,str2);
            return intervalFromNow.getDay();
        }else {
            return 0;
        }
    }

    /**
     * 判断日期是否在当前星期内
     * @param date
     * @return
     */
    public static Boolean judgeDateIsThisWeek(Date date) throws ParseException {
        Date now = new Date();
        Integer week = dayForWeek(now);//判断今天是星期几
        Integer year = getYearOfIntervalTime(date, now);
        Integer month = getMonthOfIntervalTime(date,now);
        Integer day = getDayOfIntervalTime(date,now);
        if(year == 0 && month == 0 && day < week){
            return true;
        }
        return false;
    }
    /**
     * 获取未来 第 past 天的日期
     * @param past
     * @return
     */
    public static Date getFutureDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + past);
        Date today = calendar.getTime();
        Date result =conversionDate(today);
        return result;
    }
    //字符串按照指定格式转化为日期
    public static Date stringTodate(String dateStr, String formatStr) {
        // 如果时间为空则默认当前时间
        Date date = null;
        SimpleDateFormat format = new SimpleDateFormat(formatStr);
        if (dateStr != null && !dateStr.equals("")) {
            String time = "";
            try {
                Date dateTwo = format.parse(dateStr);
                time = format.format(dateTwo);
                date = format.parse(time);
            } catch (ParseException e) {
                e.printStackTrace();
            }

        } else {
            String timeTwo = format.format(new Date());
            try {
                date = format.parse(timeTwo);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return date;
    }
    //计算两个日期相差年数
    public static int yearDateDiff(String startDate,String endDate){
        Calendar calBegin = Calendar.getInstance(); //获取日历实例
        Calendar calEnd = Calendar.getInstance();
        calBegin.setTime(stringTodate(startDate,"yyyy")); //字符串按照指定格式转化为日期
        calEnd.setTime(stringTodate(endDate,"yyyy"));
        return calEnd.get(Calendar.YEAR) - calBegin.get(Calendar.YEAR);
    }
}
